package com.msa.msaportal.controller;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.msa.msaportal.common.web.ConstantUntil;
import com.msa.msaportal.common.web.MsaJsonUntil;
import com.msa.msaportal.entity.PortalCssEntity;
import com.msa.msaportal.entity.PortalFunction;
import com.msa.msaportal.entity.PortalRole;
import com.msa.msaportal.entity.PortletViewRes;
import com.msa.msaportal.entity.Template;
import com.msa.msaportal.service.IPortalCssEntityService;
import com.msa.msaportal.service.IPortalExampleService;
import com.msa.msaportal.service.IPortalPageService;
import com.msa.msaportal.service.IPortalRoleService;
import com.msa.msaportal.service.ITemplateService;
@Controller
@RequestMapping("/portalURCms")
public class PortalUrcmsController {
	

	@Resource(name="portalManager")
	private IPortalExampleService portalManager;
	@Resource(name="portalcssManager")
	private IPortalCssEntityService portalcssManager;
	@Resource(name="templateManager")
	private ITemplateService templateManager;
	@Resource(name="pageManager")
	private IPortalPageService pageManager;
	@Resource(name="roleManager")
	private IPortalRoleService roleManager;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/addPortal.do")
	public String addPortal(ModelMap model,String userId,String cmsrole,HttpSession session) throws RemoteException{
		//从ldap获取用户信息
		String udjson = MsaJsonUntil.getJsonByUserId(userId);	
		System.out.println("cms角色::"+cmsrole);
		if(cmsrole==null){
			cmsrole="";
		}
		JSONObject jobj=JSONObject.fromObject(udjson);
		String userCn = jobj.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getString("userName");
		//String userNumber = jobj.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getString("idNumber");
		JSONArray jsonorgarry = jobj.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONArray("table");
		String orgName= "";
		for(int j=0;j<jsonorgarry.size();j++){
			if("org".equals(jsonorgarry.getJSONObject(j).getString("tableName"))){
				orgName= jsonorgarry.getJSONObject(j).getJSONArray("row").getJSONObject(0).getString("orgName");
			}
		}
		String orgCode = jobj.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getString("organizations");
		String productRoleId ="";
		JSONArray jsonarry = jobj.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONArray("table").getJSONObject(0).getJSONArray("row");
		for(int i=0;i<jsonarry.size();i++){
			
			if(ConstantUntil.APPID.equals(jsonarry.getJSONObject(i).getString("appId"))){
				productRoleId = jsonarry.getJSONObject(i).getString("productRoleId");
			}
		}

		//查询可用模板和样式
		List<Template> tlist = portalManager.getTemplatelistByorgId(orgCode);
		List<PortalCssEntity> clist = portalcssManager.getcssEntityListByorgId(orgCode);
		//查询可用页面模块资源
		List<PortletViewRes> portletViewreslist = portalManager.getportletlistByorgCode(orgCode);
		//功能权限控制
		String add_example = "";
/*		if(!"".equals(productRoleId)){
			Integer roleid = Integer.valueOf(productRoleId);
			PortalRole portalRole = roleManager.findById(roleid);
			Set<PortalFunction> portalfunction =portalRole.getPortalFunction();
			
			for(Iterator it=portalfunction.iterator();it.hasNext();){
				PortalFunction  pf = (PortalFunction) it.next();
				if(pf.getFunctionCode().equals(ConstantUntil.EXAMPLE_NEW)){
					add_example = pf.getFunctionCode();
				}
				
			}
		}*/
		if(cmsrole.indexOf(ConstantUntil.ENTITYADD)!=-1){
			add_example = ConstantUntil.EXAMPLE_NEW;
		}
		
		session.setAttribute("loginUserId", userId);
		session.setAttribute("productRoleid", productRoleId);
		model.addAttribute("userName", userCn);
		model.addAttribute("userId", userId);
		model.addAttribute("orgName", orgName);
		model.addAttribute("orgCode", orgCode);
		model.addAttribute("templatelist", tlist);
		model.addAttribute("portalcsslist", clist);
		model.addAttribute("add_example", add_example);
		model.addAttribute("portletViewreslist", portletViewreslist);
		model.addAttribute("cmsrole", cmsrole);
		return "/geteExample";
		
	}
}
